package com.wanpinghui.supplier.object.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.object.bean.ObjectDetailBean;
import com.wanpinghui.wphlibrary.model.object.bean.ParticipationBean;
import com.wanpinghui.wphlibrary.model.object.constant.ObjectStatus;
import com.wanpinghui.wphlibrary.model.object.constant.ParticipationStatus;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/28
 ***/

public class BidRecordAdapter extends BaseAdapter {
    private List<ParticipationBean> mDatas;
    private Context context;
    private int demandStatus;
    private int maxPoint;
    private UserBean userBean;
    private int connectCount;

    public BidRecordAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(List<ParticipationBean> datas, int demandStatus) {
        this.mDatas = datas;
        this.demandStatus = demandStatus;
        userBean = WPHSdk.getService(OAuthService.class).getUserInfo(null);
        notifyDataSetChanged();
    }

    public void setMaxBid(int maxPoint) {
        this.maxPoint = maxPoint;
    }

    @Override
    public int getCount() {
        connectCount = 0;
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ParticipationBean participationBean = mDatas.get(position);
        TextView textViewAvatar;
        TextView textViewName;
        TextView textViewPhone;
        TextView textViewStatus;
        TextView textViewOffer;
        View view = LayoutInflater.from(context).inflate(R.layout.item_lv_objectdetail_partic, null);
        textViewAvatar = (TextView) view.findViewById(R.id.tv_avatar);
        textViewName = (TextView) view.findViewById(R.id.tv_name);
        textViewPhone = (TextView) view.findViewById(R.id.tv_phone);
        textViewStatus = (TextView) view.findViewById(R.id.tv_status);
        textViewOffer = (TextView) view.findViewById(R.id.tv_offer);
        textViewName.setText(participationBean.getNickname());
        textViewAvatar.setText(Integer.toString(position + 1));
        String phoneStr = participationBean.getMobile();
        if (participationBean.getContact_state() == 1) {
            connectCount ++;
            textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
            if (participationBean.getContact_seconds() <= 30) {
                textViewStatus.setText("通话结束");
            } else {
                textViewStatus.setText("通话结束" + WTimeUtil.getRecordTimeStr(participationBean.getContact_seconds()));
            }
            textViewAvatar.setSelected(true);
        } else {
            if (demandStatus == ObjectStatus.STATUS_BIDDING) {
                if (position < ObjectDetailBean.MAX_SUCCESS_COUNT) {
                    textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
                    if (ObjectDetailBean.MAX_SUCCESS_COUNT < getCount()) {
                        if (mDatas.get(ObjectDetailBean.MAX_SUCCESS_COUNT).getOffer() == maxPoint) {
                            textViewStatus.setText("成就值较高，暂时领先");
                        }else {
                            textViewStatus.setText("暂时领先");
                        }
                    }else{
                        textViewStatus.setText("暂时领先");
                    }
                    textViewAvatar.setSelected(true);
                }else {
                    textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
                    textViewStatus.setText("拟出局");
                    textViewAvatar.setSelected(false);
                }
            }else {
                if (participationBean.getStatus() == ParticipationStatus.OFFER_SUCCESS_NO_FUND
                        ||participationBean.getStatus() == ParticipationStatus.OFFER_SUCCESS_NO_ATTACH) {
                    textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
                    textViewStatus.setText("待呼叫");
                    textViewAvatar.setSelected(true);
                }else if (participationBean.getStatus() == ParticipationStatus.ATTACHING) {
                    textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
                    textViewStatus.setText("正在呼叫客户");
                    textViewAvatar.setSelected(true);
                }else if (participationBean.getStatus() == ParticipationStatus.OVERTIME_NO_ATTACH) {
                    textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.color_ff001f));
                    textViewStatus.setText("超时未联系客户");
                    textViewAvatar.setSelected(true);
                }else {
                    if (connectCount == ObjectDetailBean.MAX_SUCCESS_COUNT) {
                        textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
                        textViewStatus.setText("出局");
                        textViewAvatar.setSelected(false);
                    }else {
                        textViewStatus.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
                        textViewStatus.setText("拟出局");
                        textViewAvatar.setSelected(false);
                    }
                }
            }
        }
        textViewPhone.setText(phoneStr);
        if (participationBean.getOffer() == maxPoint) {
            if (position < ObjectDetailBean.MAX_SUCCESS_COUNT) {
                textViewOffer.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryYellow));
            } else {
                textViewOffer.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
            }
            textViewOffer.setText("爆灯");
        } else {
            textViewOffer.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
            textViewOffer.setText(participationBean.getOffer() + "汇币");
        }

        if (userBean != null && userBean.getUser() != null && participationBean.getId().equals(userBean.getUser().getId())) {
            textViewName.setText("我");
        } else {
            if (demandStatus == 10) {
                textViewName.setText(participationBean.getHide_nickname());
            } else {
                textViewName.setText(participationBean.getNickname());
            }
        }
        if (demandStatus == 10) {
            textViewPhone.setVisibility(View.GONE);
        } else {
            textViewPhone.setVisibility(View.VISIBLE);
        }

        return view;
    }
}
